package com.syq.dao.impl;

import com.syq.dao.AdminDao;
import com.syq.entity.Admin;
import com.syq.mapper.AdminMapper;
import com.syq.page.PageInfo;
import com.syq.utils.GenNumTools;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("adminDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class AdminDaoImpl implements AdminDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private AdminMapper adminMapper;
    @Override
    public List<Admin>  getAllAdmins() throws Exception{
        return adminMapper.selectByExample(null);
    }


    @Override
    public Integer deleteAdminByAno(String ano)  throws Exception{
        return adminMapper.deleteByPrimaryKey(ano);
    }

    @Override
    public Integer updateAdminByAno(Admin admin)  throws Exception{
        return adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public Integer addAdmin(Admin admin)  throws Exception{
        admin.setAno(initId());
        return adminMapper.insertSelective(admin);
    }
    public String initId(){
        String maxAno = adminMapper.getMaxAno();
        return GenNumTools.initId("",3,maxAno);
    }

    @Override
    public List<Admin> getAdminByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)adminMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return adminMapper.getAdminByPage(map);
    }

    @Override
    public Integer updateAdmins(Admin admin) {
        return null;
    }

    @Override
    public String getMaxAno(){
        return adminMapper.getMaxAno();
    }
}

//
//    @Override
//    public Admin selectAdminWithChildren(String cno){
//        return adminMapper.selectAdminWithChildren(cno);
//    }
//
//    @Override
//    public Admin getAdminWithOrdrees(String cno) throws Exception {
//        return adminMapper.getAdminWithOrdrees(cno);
//    }
//
//    @Override
//    public Admin getAccountByAccountName(String cname) {
//            return adminMapper.getAccountByAccountName(cname);
//    }
//
//    /**
//     *
//     * @param cname
//     * @return
//     */
//    public List<Admin> getAdminByExample(String cname) {
//        AdminExample adminExample =new AdminExample();
//        adminExample.createCriteria().andCnameEqualTo(cname);
//        return adminMapper.selectByExample(adminExample);
//    }
//
//    @Override
//    public Integer updateAdmin(Admin admin) { return adminMapper.updateByPrimaryKey(admin); }

//    public Integer updateByPrimaryKeySelective(Admin admin) { return adminMapper.updateByPrimaryKeySelective(admin); }
//
//
//    @Override
//    public Integer updateAdmins(Admin admin) { return adminMapper.updateByPrimaryKeys(admin); }


